import { useRef } from 'react'
import { ActionType } from '@ant-design/pro-components'
import type { BasicTableParams, TableListQueryParams, TableListResponse } from '../index.type'

export function useTable() {
  const tableAction = useRef<ActionType | undefined>(null)

  function getProTableParams(params: BasicTableParams) {
    const { pageSize, current, ...args } = params
    return {
      pageNum: current,
      pageSize: pageSize,
      ...args
    } as TableListQueryParams
  }

  function getProTableResponse(resp: Partial<TableListResponse>) {
    if (resp) {
      const { data, totalCount, success } = resp
      return {
        success,
        data: data || [],
        total: totalCount
      }
    }
    return {
      success: false,
      data: []
    }
  }

  return {
    tableAction,
    getProTableParams,
    getProTableResponse
  }
}
